package cn.ac.iie.ulss.dataredistribution.commons;

import cn.ac.iie.ulss.dataredistribution.tools.HNode;
import cn.ac.iie.ulss.dataredistribution.handler.DataAccepter;
import cn.ac.iie.ulss.dataredistribution.handler.GetErrorFile;
import cn.ac.iie.ulss.dataredistribution.tools.DataProducer;
import cn.ac.iie.ulss.dataredistribution.tools.MetaStoreClientPool;
import cn.ac.iie.ulss.dataredistribution.tools.Rule;
import cn.ac.iie.ulss.dataredistribution.tools.INode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.avro.util.Utf8;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author: evan
 * @date: 2014-10-15
 */
public class GlobalVariables {

    public static final String DOCS = "docs";
    public static final String DOC_SET = "doc_set";
    public static final String DOC = "doc";
    public static final String DOC_DESC = "doc_desc";
    public static final String USER_DESC = "user_desc";
    public static final String DOC_SCHEMA_NAME = "doc_schema_name";
    public static final String DOCS_SCHEMA_CONTENT = "docsSchemaContent";
    public static final String DOC_SCHEMA_CONTENT = "docSchemaContent";
    public static final String SYN_VALUE_TO_FILE = "synValueToFile";
    public static final String SYN_STORE_USELESSDATA = "synStoreUselessData";
    public static final String SYN_STORE_UNVALIDDATA = "synStoreUnvalidData";
    public static final String SYN_DIR = "synDir";
    public static final String SYN_METASTORE_CLIENT = "synMetaStoreClient";
    public static final String FILE_TO_COUNT = "fileToCount";
    public static final String CONSUMER = "consumer";
    public static final String ERRORFILECONSUMER = "errorfileconsumer";
    public static final String METASTORE_CLIENT_POOL = "metaStoreClientPool";
    public static final String USELESS_DATA_STORE = "uselessDataStore";
    public static final String KEYWORD_TO_TYPE = "keyWordToType";
    public static final String TABLE_TO_DOC_DESC = "tableToDocDesc";
    public static final String TABLE_TO_NODES = "tableToNodes";
    public static final String TABLE_TO_SENDTHREAD_COUNT = "tableToThreadPoolSize";
    public static final String TABLE_TO_ACCEPTCOUNT = "tableToAcceptCount";
    public static final String TABLE_TO_SENDCOUNT = "tableToSendCount";
    public static final String TABLE_TO_RULE = "tableToRule";
    public static final String TABLE_TO_MSGSTATION = "tableToMSGStation";
    public static final String TABLE_TO_TOPIC = "tableToTopic";
    public static final String TOPIC_TO_DATAPOOL = "topicToDataPool";
    public static final String TOPIC_TO_PRODUCER = "topicToProducer";
    public static final String INTERVAL_TO_SNODE = "intervalToSNode";
    public static final String VALUE_TO_FILE = "valueToFile";
    static Logger logger = null;

    static {
        PropertyConfigurator.configure("log4j.properties");
        logger = Logger.getLogger(GlobalVariables.class.getName());
    }

    /**
     *
     * init the Global Variables
     */
    public static void initialize() {

        byte[] synValueToFile = new byte[0];
        RuntimeEnv.addParam(SYN_VALUE_TO_FILE, synValueToFile);

        byte[] synStoreUselssData = new byte[0];
        RuntimeEnv.addParam(SYN_STORE_USELESSDATA, synStoreUselssData);

        byte[] synStoreUnvalidData = new byte[0];
        RuntimeEnv.addParam(SYN_STORE_UNVALIDDATA, synStoreUnvalidData);

        byte[] synDir = new byte[0];
        RuntimeEnv.addParam(SYN_DIR, synDir);

        byte[] synMetaStoreClient = new byte[0];
        RuntimeEnv.addParam(SYN_METASTORE_CLIENT, synMetaStoreClient);

        logger.info("setting the tableToAcceptCount to the Global Variables");
        ConcurrentHashMap<String, AtomicLong[]> tableToAcceptCount = new ConcurrentHashMap<String, AtomicLong[]>();
        RuntimeEnv.addParam(TABLE_TO_ACCEPTCOUNT, tableToAcceptCount);

        logger.info("setting the tableToSendCount to the Global Variables");
        ConcurrentHashMap<String, AtomicLong> tableToSendCount = new ConcurrentHashMap<String, AtomicLong>();
        RuntimeEnv.addParam(TABLE_TO_SENDCOUNT, tableToSendCount);

        logger.info("setting the tableToRule to the Global Variables");
        ConcurrentHashMap<String, Rule> tableToRule = new ConcurrentHashMap<String, Rule>();
        RuntimeEnv.addParam(TABLE_TO_RULE, tableToRule);

        logger.info("setting the tableToMSGStation to the Global Variables");
        ConcurrentHashMap<String, ConcurrentHashMap<HNode, ConcurrentHashMap<INode, ConcurrentLinkedQueue>>> tableToMSGStation = new ConcurrentHashMap<String, ConcurrentHashMap<HNode, ConcurrentHashMap<INode, ConcurrentLinkedQueue>>>();
        RuntimeEnv.addParam(TABLE_TO_MSGSTATION, tableToMSGStation);

        logger.info("setting the fileToCount to the Global Variables");
        ConcurrentHashMap<Long, AtomicLong> fileToCount = new ConcurrentHashMap<Long, AtomicLong>();
        RuntimeEnv.addParam(FILE_TO_COUNT, fileToCount);

        logger.info("setting the producer to the Global Variables");
        DataAccepter accepter = new DataAccepter();
        RuntimeEnv.addParam(CONSUMER, accepter);

        logger.info("setting the errorfileconsumer to the Global Variables");
        GetErrorFile errorfileconsumer = new GetErrorFile();
        RuntimeEnv.addParam(ERRORFILECONSUMER, errorfileconsumer);

        logger.info("setting the MetaStoreClientPool to the Global Variables");
        MetaStoreClientPool mscp = null;
        RuntimeEnv.addParam(METASTORE_CLIENT_POOL, mscp);

        logger.info("setting the uselessDataStore for store to the Global Variables");
        ConcurrentHashMap<String, ConcurrentLinkedQueue> uselessDataStore = new ConcurrentHashMap<String, ConcurrentLinkedQueue>();
        RuntimeEnv.addParam(USELESS_DATA_STORE, uselessDataStore);

        logger.info("setting the keyWordToType to the Global Variables");
        ConcurrentHashMap<String, String> keyWordToType = new ConcurrentHashMap<String, String>();
        RuntimeEnv.addParam(KEYWORD_TO_TYPE, keyWordToType);

        logger.info("setting the tableToDocDesc to the Global Variables");
        ConcurrentHashMap<String, Map<Utf8, Utf8>> tableToDocDesc = new ConcurrentHashMap<String, Map<Utf8, Utf8>>();
        RuntimeEnv.addParam(TABLE_TO_DOC_DESC, tableToDocDesc);

        logger.info("setting the tableToNodes to the Global Variables");
        ConcurrentHashMap<String, ArrayList<HNode>> tableToNodes = new ConcurrentHashMap<String, ArrayList<HNode>>();
        RuntimeEnv.addParam(TABLE_TO_NODES, tableToNodes);

        logger.info("setting the tableToTopic to the Global Variables");
        ConcurrentHashMap<String, String> tableToTopic = new ConcurrentHashMap<String, String>();
        RuntimeEnv.addParam(TABLE_TO_TOPIC, tableToTopic);

        logger.info("setting the topicToDataPool to the Global Variables");
        Map<String, ConcurrentLinkedQueue[]> topicToDataPool = new HashMap<String, ConcurrentLinkedQueue[]>();
        RuntimeEnv.addParam(TOPIC_TO_DATAPOOL, topicToDataPool);

        logger.info("setting the topicToProducer to the Global Variables");
        Map<String, DataProducer> topicToProducer = new HashMap<String, DataProducer>();
        RuntimeEnv.addParam(TOPIC_TO_PRODUCER, topicToProducer);

        logger.info("setting the tableToThreadPoolSize to the Global Variables");
        ConcurrentHashMap<String, AtomicLong> tableToSendThreadCount = new ConcurrentHashMap<String, AtomicLong>();
        RuntimeEnv.addParam(TABLE_TO_SENDTHREAD_COUNT, tableToSendThreadCount);    

        logger.info("setting the valueToFile to the Global Variables");
        ConcurrentHashMap<String, Object[]> valueToFile = new ConcurrentHashMap<String, Object[]>();
        RuntimeEnv.addParam(VALUE_TO_FILE, valueToFile);
        
        logger.info("setting the tableToThreadPoolSize to the Global Variables");
        ConcurrentHashMap<String, INode> intervalToSNode = new ConcurrentHashMap<String, INode>();
        RuntimeEnv.addParam(INTERVAL_TO_SNODE, intervalToSNode);
    }
}
